/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.mrd.gtimp.business.service.cargoDeclaration.mapper;

import com.mrd.gtimp.business.entity.Commodity;
import com.mrd.gtimp.business.entity.Declaration;

import org.apache.commons.collections.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bean.decla.DeclaCertificate;
import bean.decla.DeclaCustoms;
import bean.decla.DeclaGood;
import bean.decla.EcoRelation;
import bean.decla.sub.ExportCustomsHead;
import bean.decla.sub.ExportGoodCustoms;

/**
 * Created by dengdc on 2017/5/25.
 */
public class ExportDelcarationMapper extends BaseDeclarationMapper {

    /**
     *
     * @param dn
     * @return
     */
    protected DeclaCustoms createDeclaCustoms(Declaration dn){
        DeclaCustoms declaCustoms = new DeclaCustoms();
        ExportCustomsHead exportCustomsHead = createCustomsHead(dn);
        declaCustoms.setCustomsHead(exportCustomsHead);
        //海关随附单据信息
        List<DeclaCertificate> certificateList = createDeclaCerList(dn);
        declaCustoms.setDeclaCertificates(certificateList);
        List<EcoRelation> ecoRelationList = createEcoRelations(dn);
        declaCustoms.setEcoRelations(ecoRelationList);
        declaCustoms.setDeclaTrn(createTrn(dn));
        return declaCustoms;
    }

    /**
     *
     * @param dn
     * @return
     */
    private ExportCustomsHead createCustomsHead(Declaration dn){
        SimpleDateFormat yy_MM_dd = new SimpleDateFormat("yyyy-MM-dd");
        ExportCustomsHead exportCustomsHead = new ExportCustomsHead();
        //申报口岸代码
        exportCustomsHead.setDeclPort(dn.getDeclaration_port_code());
        //合同协议号
        exportCustomsHead.setContractNo(dn.getContract_no());
        //提运单号
        exportCustomsHead.setBillNo(dn.getBl_no());
        //运输工具名称
        exportCustomsHead.setTrafName(dn.getTransport_na());
        //航次号
        exportCustomsHead.setVoyageNo(dn.getVoyage_no());
        //出口口岸代码
        exportCustomsHead.setiEPort(dn.getExport_port_code());
        //备案号
        exportCustomsHead.setManualNo(dn.getRecord_id());
        //出口日期
        Date exportDate = dn.getExport_date();
        String exportDateStr = null;
        if(exportDate != null){
            exportDateStr = yy_MM_dd.format(exportDate);
        }
        exportCustomsHead.setiEDate(exportDateStr);
        //收发货人代码
        exportCustomsHead.setTradeCo(dn.getCompany_co());
        //收发货人名称
        exportCustomsHead.setTradeName(dn.getCompany_name());
        //运输方式代码
        exportCustomsHead.setTrafMode(dn.getTransport_mode_code());
        //生产销售单位编码
        exportCustomsHead.setOwnerCode(dn.getFahuo_company_co());
        //生产销售单位名称
        exportCustomsHead.setOwnerName(dn.getFahuo_company_name());
        //申报单位代码
        exportCustomsHead.setAgentCode(dn.getShenbao_company_co());
        //申报单位名称
        exportCustomsHead.setAgentName(dn.getShenbao_company_name());
        //征免性质代码
        exportCustomsHead.setCutModel(dn.getLevymode_code());
        //结汇方式代码
        exportCustomsHead.setPayModel(null);
        //纳税单位
        exportCustomsHead.setPaymentMark(dn.getTex_company());
        //监管方式代码
        exportCustomsHead.setTradeMode(dn.getTrade_mode_code());
        //许可证号
        exportCustomsHead.setLicense(dn.getLicense_no());
        //抵运国（地区）代码
        exportCustomsHead.setTradeCountry(dn.getArrived_country_code());
        //指运港代码
        exportCustomsHead.setDestinationPort(dn.getArrived_port_code());
        //境内货源地代码
        exportCustomsHead.setDestinationCode(dn.getGoods_addr_code());
        //件数
        exportCustomsHead.setPackNo(dn.getGoods_count());
        //包装种类代码
        exportCustomsHead.setWrapType(turnPackageType(dn.getGoods_package()));
        //毛重
        exportCustomsHead.setGrossWt(dn.getGross_weight());
        //净重
        exportCustomsHead.setNetWt(dn.getNet_weight());
        //运费标记
        exportCustomsHead.setFeeMark(dn.getFreight1Code());
        //运费/率
        exportCustomsHead.setFeeRate(dn.getFreight2());
        //运费币制代码
        exportCustomsHead.setFeeCurr(dn.getFreight3Code());
        //保费标记
        exportCustomsHead.setInsurMark(dn.getPremium1Code());
        //保费/率
        exportCustomsHead.setIsurRate(dn.getPremium2());
        //保费币制代码
        exportCustomsHead.setInsurCurr(dn.getPremium3Code());
        //杂费标记
        exportCustomsHead.setOtherMark(dn.getOtherfee1Code());
        //杂费/率
        exportCustomsHead.setOtherRate(dn.getOtherfee2());
        //杂费币制代码
        exportCustomsHead.setOtherCurr(dn.getOtherfee3Code());
        //报关单类型代码
        exportCustomsHead.setEntryType(turnEntryType(dn.getCustoms_type()));
        //成交方式代码
        exportCustomsHead.setTransMode(turnTransMode(dn.getDeal_mode()));
        //批准文号
        exportCustomsHead.setApprNo(dn.getApproval_no());
        //报关备注
        exportCustomsHead.setNoteS(dn.getRemark());
        //关联报关单号
        exportCustomsHead.setRelativeId(dn.getRe_manifest());
        //关联备案号
        exportCustomsHead.setRelativeManualNo(dn.getRe_record_id());
        //监管仓号
        exportCustomsHead.setBondedNo(dn.getRe_jianguan());
        //货场代码
        exportCustomsHead.setCustomsField(dn.getRe_yard_id());
        //转关提前报关
        exportCustomsHead.setEntryTransitType(turnDeclaType(dn.getData_type()));
        //报关员名称
        exportCustomsHead.setEntryPerson(null);
        //报关员联系方式
        exportCustomsHead.setEntryPersonContact(null);
        //收发货人统一编码
        exportCustomsHead.setTradeCodeScc(dn.getCompany_co_unite());
        //申报单位统一编码
        exportCustomsHead.setAgentCodeScc(dn.getShenbao_company_co_unite());
        //消费使用单位统一编码
        exportCustomsHead.setOwnerCodeScc(dn.getFahuo_company_co_unite());
        //录入单位统一编码
        exportCustomsHead.setCopCodeScc(dn.getCompany_co_unite());
        //特殊关系确认
        exportCustomsHead.setSpecialRelation(revert01(dn.getSpecial_relationship_confirm()));
        //价格影响关系确认
        exportCustomsHead.setPriceEffect(revert01(dn.getPrice_impact_confirm()));
        //支付特使权使用费确认
        exportCustomsHead.setPaySpecialEnvoy(revert01(dn.getPayment_royaltils_confirm()));
        //贸易国（地区）
        exportCustomsHead.setTradeAreaCode(dn.getTrading_country_code());

        return exportCustomsHead;
    }
    protected List<DeclaGood> createDeclaGoods(List<Commodity> commodities){
        List<DeclaGood> declaGoods = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(commodities)){
            for (Commodity cm : commodities) {
                DeclaGood declaGood = new DeclaGood();
                ExportGoodCustoms exportGoodCustoms = new ExportGoodCustoms();
                //商品HS编码
                exportGoodCustoms.setHsCode(cm.getCode_t()+cm.getCode_s());
                //商品中文名称
                exportGoodCustoms.setGoodsCn(cm.getName());
                //规格型号
                exportGoodCustoms.setGoodsModel(cm.getGoods_mode());
                //总价
                exportGoodCustoms.setAmount(cm.getTotal_price());
                //单价
                exportGoodCustoms.setPrice(cm.getUnit_price());
                //币制
                exportGoodCustoms.setTradeCurr(cm.getCurrencyCode());
                //目的国代码
                exportGoodCustoms.setOriginCountry(cm.getDestination_code());
                //第一计量数量
                exportGoodCustoms.setQty1(cm.getLegal_count());
                //第一计量单位代码
                exportGoodCustoms.setUnit1(cm.getLegalUnitCode());
                //申报数量
                exportGoodCustoms.setgQty(cm.getCount());
                //申报计量单位代码
                exportGoodCustoms.setgUnit(cm.getUnitCode());
                //第二计量数量
                exportGoodCustoms.setQty2(cm.getSecond_count());
                //第二计量单位代码
                exportGoodCustoms.setUnit2(cm.getSecondUnitCode());
                //征免代码
                exportGoodCustoms.setDutyMode(cm.getLevyModeCode());
                //版本号
                exportGoodCustoms.setPrdtNo(cm.getVersion_no());
                //货号
                exportGoodCustoms.setGoodsId(cm.getGoods_no());
                //工缴费
                exportGoodCustoms.setProcessingFees(cm.getProcessing_fee());
                //生产厂家
                exportGoodCustoms.setManufactureFactory(cm.getProducer());
                //原产国
                exportGoodCustoms.setDestinationCountry(cm.getOrigin_country_code());
                //备案序号
                exportGoodCustoms.setContrItem(cm.getRecord_id());
                declaGood.setGoodCustoms(exportGoodCustoms);
                declaGoods.add(declaGood);
            }
        }
        return declaGoods;
    }

}
